import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes.config'
import store from '../store'
// import * as types from '../store/mutations-type'

Vue.use(VueRouter);

const router = new VueRouter({
    routes,
});

// 钩子
router.beforeEach((to, from, next) => {
    //登录判断
    if (to.meta.requireAuth && !store.getters.sid) {
        // utils.toast.error("请先登录");
        router.push({name: 'login'});
    }
    // store.commit(types.SET_FORM_ROUTER, from.name);
    // 判断登录
    if (to.meta.auth && !store.getters.token) next({path: '/login'});
    next();

    let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
    // window.sessionStorage.setItem('scrollTop_', scrollTop);
    next();
});

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router;
